package com.github.grpc.example;

import com.github.grpc.proto.GreeterGrpc;
import com.github.grpc.proto.HelloReply;
import com.github.grpc.proto.HelloRequest;
import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;

public class HelloClient {
    private Channel channel;
    private GreeterGrpc.GreeterBlockingStub blockingStub;
    private GreeterGrpc.GreeterStub asyncStub;

    public HelloClient(String host, int port) {
        /*
        * NegotiationType 用于定义启动HTTP/2的协商方式：
        * TLS： 使用 TLS ALPN/NPN 协商，假定是 SSL 连接
        * PLAINTEXT_UPGRADE： 使用 HTTP 升级协议为 plaintext(非SSL)从 HTTP/1.1 升级到 HTTP/2
        * PLAINTEXT: 假设连接是 plaintext(非SSL) 而且远程端点直接支持HTTP2.而不需要升级
        * */
        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
    }

    public HelloClient(ManagedChannelBuilder<?> channelBuilder) {
        this.channel = channelBuilder.build();
        this.blockingStub = GreeterGrpc.newBlockingStub(channel);
        this.asyncStub = GreeterGrpc.newStub(channel);
    }

    public HelloReply sayHello(HelloRequest request) {
        return this.blockingStub.sayHello(request);
    }
}
